//
//  EAppSumChartPanl.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-9.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

#import "EAppSumChartPanl.h"
#import "mcf.h"

#import <UIKit/UIKit.h>
#import "MIMLineGraph.h"

@interface EAppSumChartPanl()<LineGraphDelegate>
{
    MIMLineGraph * mLineGraph_;
    
    NSArray * yValuesArray;
    
    NSDictionary * verticalLinesProperties;
    NSDictionary * horizontalLinesProperties;
    
    NSArray * xValuesArray;
    NSArray * xTitlesArray;
    
    NSArray *anchorPropertiesArray;
    

}

@property (nonatomic, strong) MIMLineGraph * mLineGraph;

@property (nonatomic, strong) NSArray * yValuesArray;
@property (nonatomic, strong) NSDictionary * verticalLinesProperties;
@property (nonatomic, strong) NSDictionary * horizontalLinesProperties;

@property (nonatomic, strong) NSArray * xValuesArray;
@property (nonatomic, strong) NSArray * xTitlesArray;

@property (nonatomic, strong)     NSArray *anchorPropertiesArray;
@property (nonatomic, retain) UIButton * detailRankingButton;


- (void) refreshGraphWithRanking:(ERanking *) ranking;


@end



@implementation EAppSumChartPanl

@synthesize mLineGraph = mLineGraph_;

@synthesize yValuesArray;
@synthesize verticalLinesProperties;
@synthesize horizontalLinesProperties;

@synthesize xValuesArray;
@synthesize xTitlesArray;

@synthesize anchorPropertiesArray;


- (void) dealloc
{
    YRelease(mLineGraph_);
    YRelease(yValuesArray)
    
    YRelease(verticalLinesProperties)
    YRelease(horizontalLinesProperties)
    
    YRelease(xValuesArray)
    YRelease(xTitlesArray);
    
    YRelease(anchorPropertiesArray)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
}

- (void) reAdjustLayout
{
//    [self.mLineGraph setFrame:CGRectMake(0, 0, self.width, self.height)];
    
    [self.mLineGraph drawMIMLineGraph];
    [self.detailRankingButton setFrame:CGRectMake(self.width - 60, 20, 50, 40)];
}


- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    [self setBackgroundColor:COLOR_SCROLLVIEWTEXTUREDBACKGROUND];

    self.detailRankingButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    BUTTON_FUNCTION(self.detailRankingButton, detailRanking);
    
    [self addSubview:self.detailRankingButton];
    
    self.bInited = TRUE;
}

- (void) detailRanking
{
    DELEGATE_CALLBACK(detailRanking:, nil);
}


- (void) setAppRanking:(ERanking *)appRankingValue
{
    YRelease(_appRanking)
    _appRanking = [appRankingValue retain];
    
    [self refreshGraphWithRanking:appRankingValue];
}



- (void) refreshGraphWithRanking:(ERanking *) ranking
{
    if (!ranking)
    {
        return;
    }
    
    // to refrseh graphview ...
    
    [self.mLineGraph removeFromSuperview];
    YRelease(self.mLineGraph)
    
    MIMLineGraph * temp_LineGraph = [[MIMLineGraph alloc] init];
    
    [temp_LineGraph setFrame:CGRectMake(-15, 70, self.width, self.height - 80)];
    [temp_LineGraph setDelegate:self];
    
    self.mLineGraph = temp_LineGraph;
    YRelease(temp_LineGraph);
    
    self.horizontalLinesProperties=nil;
    self.anchorPropertiesArray=nil;

    NSMutableArray * xArrays = [[NSMutableArray alloc] initWithCapacity:[ranking.rankingValues count]];
    NSMutableArray * yAppRankingArray = [[NSMutableArray alloc] initWithCapacity:[ranking.rankingValues count]];
    
    int i = 0;
    for (ERankingPoint * point in ranking.rankingValues)
    {
        if (!point)
        {
            continue;
        }
        
        [xArrays addObject:[NSString stringWithFormat:@"%d 点", i++]];
        [yAppRankingArray addObject: NUMBER(point.yValue)];
    }
    self.xTitlesArray = xArrays;
    self.xValuesArray = xArrays;
    
//    int addtionValue = [[yAppRankingArray lastObject] intValue];
    
    [yAppRankingArray addObject:NUMBER(0)];
    
    self.yValuesArray = [NSArray arrayWithObjects:yAppRankingArray, nil];

    YRelease(xArrays);
    YRelease(yAppRankingArray);


    self.mLineGraph.delegate=self;
    self.mLineGraph.xTitleStyle=X_TITLES_STYLE2;
    [self.mLineGraph.titleLabel setText:ranking.subTitle];
    self.mLineGraph.topMargin = 20;
    self.mLineGraph.bottomMargin=30;
    self.mLineGraph.rightMargin=5;
    self.mLineGraph.leftMargin=5;
    
    [self addSubview:self.mLineGraph];
    
    [self reAdjustLayout];
    
}

#pragma mark - LineGraphDelegate
-(NSArray *)valuesForGraph:(id)graph
{
    return self.yValuesArray;
}

-(NSArray *)valuesForXAxis:(id)graph
{
    return self.xValuesArray;
}

-(NSArray *)titlesForXAxis:(id)graph
{
    
    return self.xTitlesArray;
    
}

-(NSArray *)AnchorProperties:(id)graph
{
    return self.anchorPropertiesArray;
}

-(NSDictionary *)horizontalLinesProperties:(id)graph
{
    return self.horizontalLinesProperties;
    
}

-(NSDictionary*)verticalLinesProperties:(id)graph
{
    return self.verticalLinesProperties;
}


@end
